package net.easyjava.third.search.utils;

import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.search.annotation.Index;
import net.easyjava.third.search.annotation.IndexID;
import net.easyjava.third.search.exception.SearchException;
import net.easyjava.tools.lang.HanyuTools;
import net.easyjava.tools.object.ObjectTools;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxnacy on 16/7/7.
 */
public class SearchUtils {
    private static Logger logger = LoggerFactory.getLogger(SearchUtils.class);


    public static String getIndex(Object indexObj){
        Class clazz = indexObj.getClass();
        return getIndex(clazz);
    }
    /**
     * 获取索引名称
     * @param clazz
     * @return
     */
    public static String getIndex(Class clazz){
        boolean flag = clazz.isAnnotationPresent(Index.class);
        if(!flag){
            throw new SearchException("obj don't has Index");
        }
        Index index = (Index) clazz.getAnnotation(Index.class);
        return index.index();
    }


    public static String getType(Object indexObj){
        Class clazz = indexObj.getClass();
        return getType(clazz);
    }
    /**
     * 获取type
     * @param clazz
     * @return
     */
    public static String getType(Class clazz){
        boolean flag = clazz.isAnnotationPresent(Index.class);
        if(!flag){
            throw new SearchException("obj don't has Index");
        }
        Index index = (Index) clazz.getAnnotation(Index.class);
        return index.type();
    }

    /**
     * 获取索引id
     * @param indexObj
     * @return
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    public static String getId(Object indexObj) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        String id = null;
        Class clazz = indexObj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        boolean flag = false;
        for(Field field:fields){
            flag = field.isAnnotationPresent(IndexID.class);
            if(flag){
                IndexID ID = field.getAnnotation(IndexID.class);
                id = ObjectTools.invokeGetMethod(indexObj,field.getName()).toString();
                return id;
            }
        }

        if(!flag){
            throw new SearchException("obj don't has IndexID");
        }
        return null;
    }

    /**
     * 将搜索结果序列化为list
     * @param searchResponse
     * @param indexName
     * @param clazz
     * @param queryWord 传值则返回结果附带
     * @param <T>
     * @return
     */
    public static <T> List<T> getListFromResponse(SearchResponse searchResponse,String indexName,
                                                  Class<T> clazz,String queryWord,boolean needHighLighter){
        List<T> list = new ArrayList<>();
        SearchHits shs = searchResponse.getHits();
        SearchHit[] searchHits = shs.getHits();
        if(searchHits.length>0){
            for(SearchHit searchHit:searchHits){

                String id = searchHit.getId();
                if(searchHit.isSourceEmpty()){
                    logger.error("索引:{} id:{} 内容为空",indexName,searchHit.getId());
                    continue;
                }
                String source = searchHit.getSourceAsString();

//                if(needHighLighter && !StringTools.isEmpty(queryWord)){
//                    source = JsonTools.toHighLighter(source, queryWord);
//                }
                list.add(JSONObject.parseObject(source, clazz));
            }
        }
        return list;
    }

    /**
     * 将object中的文字转为拼音添加到json中
     * @param object
     * @return
     */
    public static JSONObject putPinyinIn(Object object){
        String jsonSource  = JSONObject.toJSONString(object);
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(object));
        jsonObject.put("pinyin", HanyuTools.toPinyin(jsonSource));
        return jsonObject;
    }

    public static JSONObject putPinyinIn(String json){
        JSONObject jsonObject = JSONObject.parseObject(json);
        jsonObject.put("pinyin", HanyuTools.toPinyin(json));
        return jsonObject;
    }
}
